package com.csii.modules.online.cgreport.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csii.core.system.query.QueryGenerator;
import com.csii.core.util.DateUtils;
import com.csii.core.util.oConvertUtils;
import com.csii.modules.online.cgform.util.CgformSqlUtil;
import com.csii.modules.online.cgreport.entity.OnlCgreportItem;
import com.csii.modules.online.config.exception.DBException;
import com.csii.modules.online.config.util.DialectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class a {
	private static final Logger log = LoggerFactory.getLogger(a.class);

	public static void a(HttpServletRequest var0, Map<String, Object> var1, Map<String, Object> var2, Map<String, Object> var3) {
		String field_name = (String) var1.get("field_name");
		String search_mode = (String) var1.get("search_mode");
		String field_type = (String) var1.get("field_type");
		String var7;
		String var8;
		String var9;
		if ("single".equals(search_mode)) {
			var7 = var0.getParameter(field_name.toLowerCase());

			try {
				if (oConvertUtils.isEmpty(var7)) {
					return;
				}

				var8 = var0.getQueryString();
				if (var8.contains(field_name + "=")) {
					var9 = new String(var7.getBytes("ISO-8859-1"), "UTF-8");
					var7 = var9;
				}
			} catch (UnsupportedEncodingException var10) {
				log.error(var10.getMessage(), var10);
				return;
			}

			if (oConvertUtils.isNotEmpty(var7)) {
				if (var7.contains("*")) {
					var7 = var7.replaceAll("\\*", "%");
					var2.put(field_name, " LIKE :" + field_name);
				} else {
					var2.put(field_name, " = :" + field_name);
				}
			}

			var3.put(field_name, a(field_type, var7, true));
		} else if ("group".equals(search_mode)) {
			var7 = var0.getParameter(field_name.toLowerCase() + "_begin");
			var8 = var0.getParameter(field_name.toLowerCase() + "_end");
			if (oConvertUtils.isNotEmpty(var7)) {
				var9 = " >= :" + field_name + "_begin";
				var2.put(field_name, var9);
				var3.put(field_name + "_begin", a(field_type, var7, true));
			}

			if (oConvertUtils.isNotEmpty(var8)) {
				var9 = " <= :" + field_name + "_end";
				var2.put(new String(field_name), var9);
				var3.put(field_name + "_end", a(field_type, var8, false));
			}
		}

	}

	private static Object a(String var0, String var1, boolean var2) {
		Object var3 = null;
		if (oConvertUtils.isNotEmpty(var1)) {
			if ("String".equalsIgnoreCase(var0)) {
				var3 = var1;
			} else if ("Date".equalsIgnoreCase(var0)) {
				if (var1.length() != 19 && var1.length() == 10) {
					if (var2) {
						var1 = var1 + " 00:00:00";
					} else {
						var1 = var1 + " 23:59:59";
					}
				}

				SimpleDateFormat var4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				var3 = DateUtils.str2Date(var1, var4);
			} else if ("Double".equalsIgnoreCase(var0)) {
				var3 = var1;
			} else if ("Integer".equalsIgnoreCase(var0)) {
				var3 = var1;
			} else {
				var3 = var1;
			}
		}

		return var3;
	}

	public static String a(List<Map<String, Object>> var0, Long var1) {
		JSONObject var2 = new JSONObject();
		JSONArray var3 = new JSONArray();
		var2.put("total", var1);
		if (var0 != null) {
			Iterator var4 = var0.iterator();

			while (var4.hasNext()) {
				Map var5 = (Map) var4.next();
				JSONObject var6 = new JSONObject();

            String var8;
            String var9;
            for(Iterator var7 = var5.keySet().iterator(); var7.hasNext(); var6.put(var8, var9)) {
               var8 = (String)var7.next();
               var9 = String.valueOf(var5.get(var8));
               var8 = var8.toLowerCase();
               if (var8.contains("time") || var8.contains("date")) {
                  var9 = a(var9);
               }
            }

            var3.add(var6);
         }
      }

      var2.put("rows", var3);
      return var2.toString();
   }

	public static String a(List<Map<String, Object>> var0) {
		JSONArray var1 = new JSONArray();
		Iterator var2 = var0.iterator();

		while (var2.hasNext()) {
			Map var3 = (Map) var2.next();
			JSONObject var4 = new JSONObject();

			String var6;
			String var7;
			for (Iterator var5 = var3.keySet().iterator(); var5.hasNext(); var4.put(var6, var7)) {
				var6 = (String) var5.next();
				var7 = String.valueOf(var3.get(var6));
				var6 = var6.toLowerCase();
				if (var6.contains("time") || var6.contains("date")) {
					var7 = a(var7);
				}
			}

			var1.add(var4);
		}

		return var1.toString();
	}

	public static String a(String var0) {
		SimpleDateFormat var1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		SimpleDateFormat var2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date var3 = null;

		try {
			var3 = var1.parse(var0);
			return var2.format(var3);
		} catch (Exception var5) {
			return var0;
		}
	}

	/**
	 * @param dataLiat  var0
	 * @param param     var1
	 * @param tableName var2
	 * @return
	 */
	public static String a(List<OnlCgreportItem> dataLiat, Map<String, Object> param, String tableName) {
		StringBuffer var3 = new StringBuffer();
		String var4 = "";

		try {
			var4 = DialectUtil.getDatabaseType();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (DBException e) {
			e.printStackTrace();
		}

		Iterator var5 = dataLiat.iterator();

		while (true) {
			while (true) {
				String var7;
				String var8;
				Object var13;
				do {
					while (true) {
						OnlCgreportItem var6;
						do {
							if (!var5.hasNext()) {
								return var3.toString();
							}

							var6 = (OnlCgreportItem) var5.next();
							var7 = var6.getFieldName();
							var8 = var6.getFieldType();
						} while (1 != var6.getIsSearch());

						Object var9;
						if ("group".equals(var6.getSearchMode())) {
							var9 = param.get(var7 + "_begin");
							if (var9 != null) {
								var3.append(" and " + tableName + var7 + " >= ");
								if (!"Long".equals(var8) && !"Integer".equals(var8)) {
									if ("ORACLE".equals(var4)) {
										if (var8.toLowerCase().equals("datetime")) {
											var3.append(CgformSqlUtil.toDateTimeSql(var9.toString()));
										} else if (var8.toLowerCase().equals("date")) {
											var3.append(CgformSqlUtil.toDateSql(var9.toString()));
										}
									} else {
										var3.append("'" + var9.toString() + "'");
									}
								} else {
									var3.append(var9.toString());
								}
							}

							var13 = param.get(var7 + "_end");
							break;
						}

						var9 = param.get(var7);
						if (var9 != null) {
							String var10 = QueryGenerator.getSingleQueryConditionSql(var7, tableName, var9, !"Long".equals(var8) && !"Integer".equals(var8));
							var3.append(" and " + var10);
						}
					}
				} while (var13 == null);

				var3.append(" and " + tableName + var7 + " <= ");
				if (!"Long".equals(var8) && !"Integer".equals(var8)) {
					if ("ORACLE".equals(var4)) {
						if (var8.toLowerCase().equals("datetime")) {
							var3.append(CgformSqlUtil.toDateTimeSql(var13.toString()));
						} else if (var8.toLowerCase().equals("date")) {
							var3.append(CgformSqlUtil.toDateSql(var13.toString()));
						}
					} else {
						var3.append("'" + var13.toString() + "'");
					}
				} else {
					var3.append(var13.toString());
				}
			}
		}
	}
}
